const fs = require("fs");
const path = require("path");
const express = require("express");

const app = express();
const dirName = path.join(__dirname, "assets");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "assets")));
app.post("/common", function (req, res) {
  console.log("收到post请求了");
  //获取url中的请求参数
  var query = req.query;
  console.log(query, "queryquery");
  //在Express中没有内置获取表单post请求的api，
  //这里我们需要使用一个第三方包 body-parser
  var params = req.body;
  res.send({
    data: "12",
  });
  console.log(params, "paramsparams");
});
app.use(function (req, res, next) {
  const html = fs.readFileSync(`${dirName}/index.html`);
  res.setHeader("Content-Type", "text/html;charset=utf-8");
  res.end(html);
});

app.listen(2000, () => {
  console.log(`app listening at http://localhost:2000`);
});
